<template>
    <div>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="用户名" prop="admin_name">
                <el-input v-model="form.admin_name"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="admin_password">
                <el-input v-model="form.admin_password" type="password"></el-input>
            </el-form-item>
            <el-form-item label="联系方式" prop="admin_phone">
                <el-input v-model="form.admin_phone"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <el-input type="textarea" v-model="form.remark"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="addAdmin" v-if="!disable">立即创建</el-button>
                <el-button type="primary" @click="saveAdmin" v-else>保存信息</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
export default {
    name:'admin-editor',
    props:{
        form:{
            type:Object
        },
        disable:{
            type:Boolean
        }
    },
    data(){
        return{
            rules:{
                admin_name:[
                    { required: true, message: '请输入用户名', trigger: 'blur' }
                ],
                admin_password:[
                    { required: true, message: '请输入密码', trigger: 'blur' }
                ],
                admin_phone:[
                    { required: true, message: '请输入联系方式', trigger: 'blur' }
                ],
            }
        }
    },
    methods:{
        addAdmin(){
            console.log(this.form)
            this.$http.post("/api/admin/addAdmin",this.form).then((res)=>{
                console.log(res);
                if(res.status == 200){
                    this.$message({
                        type: 'success',
                        message: '创建成功!'
                    });
                    this.$emit('closeDialog');
                    this.$emit('refresh');
                }
            })
        },
        saveAdmin(){
            this.$http.post("/api/admin/editAdmin",this.form).then((res)=>{
                console.log(res);
                if(res.status == 200){
                    this.$message({
                        type: 'success',
                        message: '保存成功!'
                    });
                    this.$emit('closeDialog');
                    this.$emit('refresh');
                }
            })
        }
    }
}
</script>